//index.js
//获取应用实例
// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    input:0,//鼠标聚焦
    keyword:'',

    question: [], //从数据库拿到的问题信息
    quesdisplay: [], //显示的问题信息
    start: 0, //渲染开始位置
    skipNum: 10, //追加数据数量
    count: 0, //问题总数

    //筛选
    all: 1,
    myFaculty: 0,
    comment: 0,
    collect: 0, //1上2下
  },

  //鼠标聚焦
  focus:function(){
    this.setData({
      input:1
    })
  },

  //鼠标移开
  blur:function(){
    this.setData({
      input:0
    })
  },
  //搜索
  input:function(e){
    var that = this
    that.setData({
      keyword:e.detail.value
    })
  },
  search:function(){
     var that = this
     wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url:  'https://www.cerywxr.com/treehole/index.php/Home/ques/search_all_qes',
      data: { 
        keyword: that.data.keyword
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {
          
          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            //筛选
            all: 1,
            myFaculty: 0,
            comment:0,
            collect: 0, //1上2下
          })
        } else if(res.data.error_code == 1){
          wx.showModal({
            title: '提示!',
            content: '搜索内容不得为空',
            showCancel: false,
            success(res) { }
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀~!',
          content: '网络状态不佳呢！',
          showCancel: false,
          success(res) { }
        })
      },
      complete: function (res) {   //注意！
        wx.hideLoading()
      }

    })
  },

  /**
   * 筛选
   */
  //全部
  All: function () {
    var that = this

    //服务器交互
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/getAllQes',
      data: {},
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {
          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            //筛选
            all: 1,
            myFaculty: 0,
            comment: 0,
            collect: 0, //1上2下
          })

        } else {
          console.log('qes页面', res.data)
        }
      }
    })
  },
  //本学校
  Faculty: function () {
    var that = this

    //服务器交互
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/screenFaculty',
      data: {
        user_school: getApp().globalData.userInfo.user_school
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {
            
          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            //筛选
            all: 0,
            myFaculty: 1,
            comment: 0,
            collect: 0, //1上2下
          })

        } else {
          console.log('qes页面', res.data)
        }
      }
    })
  },
  //回答
  Comment: function () {
    var that = this
    var ord = '正'
    if (that.data.comment != 1) {
      that.setData({
        comment: 1
      })
    } else {
      that.setData({
        comment: 2
      })
      ord = '倒'
    }
    //与服务器交互
    //服务器交互
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/comment_num',
      data: {
        order: ord,
        mySchool:that.data.myFaculty,
        user_school: getApp().globalData.userInfo.user_school
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {

          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            //筛选
            collect: 0, //1上2下
          })

        } else {
          console.log('qes页面', res.data)
        }
      }
    })
  },
  //关注

  Collect: function () {
    var that = this
    var ord = '正'
    if (that.data.collect != 1) {
      that.setData({
        collect: 1
      })
    } else {
      that.setData({
        collect: 2
      })
      ord = '倒'
    }
    //与服务器交互
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/collect_num',
      data: {
        order: ord,
        mySchool:that.data.myFaculty,
        user_school: getApp().globalData.userInfo.user_school
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {

          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            //筛选
            comment: 0, //1上2下
          })

        } else {
          console.log('qes页面', res.data)
        }
      }
    })
  },

   //触底刷新，获取更多
   onReachBottom:function(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var end=that.data.start+that.data.skipNum
    var newData=that.data.question.slice(that.data.start,end)
    that.setData({
      quesdisplay:that.data.quesdisplay.concat(newData),//拼接数组
      start:end
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 500);//计时器  
  },

  //上拉刷新
  refresh: function () {
    var that = this
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '刷新中...',
    })

    if (that.data.all) {
      that.All()
    } else if (that.data.myFaculty) {
      that.Faculty()
    } else if (that.data.comment == 1) {
      that.setData({
        comment: 2
      })
      that.Comment()
    } else if (that.data.comment == 2) {
      that.setData({
        comment: 1
      })
      that.Comment()
    } else if (that.data.collect == 1) {
      that.setData({
        collect: 2
      })
      that.Collect()
    } else if (that.data.collect == 2) {
      that.setData({
        collect: 1
      })
      that.Collect()
    }
    setTimeout(function () {
      //隐藏loading 提示框
      wx.hideLoading();
      //隐藏导航条加载动画
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000)

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options){
    try {
      var value = wx.getStorageSync('user_openid')
      if (value) {
       
        wx.request({
          url:  'https://www.cerywxr.com/treehole/index.php/Home/user/login',
          data: {
            user_openid: value
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success(res) {
            if (res.data.error_code != 0) {
              //打印错误信息
                console.log(res.data)
            } else {
              //存用户信息
              getApp().globalData.userInfo = res.data.data
             
              //跳转至带有底部导航栏的页面
              wx.switchTab({
                url: '/pages/qes/qes',
              })
            }
          },
          fail: function (res) {
            wx.showModal({
              title: '哎呀~!',
              content: '网络状态不佳呢！',
              showCancel: false,
              success(res) { }
            })
          },
          complete: function (res) {   //注意！
          }
        })
      }
    } catch (e) {
      console.log(e)
    }
  },
  
  onShow: function (options) {
    var that = this

    //服务器交互
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/getAllQes',
      data: {},
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {
          if (res.data.count > 100) {
            that.setData({ //必须用setData，因为视图层和逻辑层相互独立，并不会因为逻辑层的数据改变而改变，所以必须用setData刷新数据
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,
            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
          })

        } else {
          console.log('qes页面', res.data)
        }
      }
    })
  },
})